package com.itstu.gulimall.order.controller;

import com.itstu.gulimall.order.entity.OrderEntity;
import com.itstu.gulimall.order.entity.OrderReturnReasonEntity;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.UUID;

/**
 * @author shhjiang_001
 * @create 2020-06-01 19:43
 */
@Controller
public class RabbitController {
    @Autowired
    RabbitTemplate rabbitTemplate;
    @GetMapping("/sendMq")
    @ResponseBody
    public String sendMessageTest(@RequestParam(value = "num",defaultValue = "10") Integer num) {
        for (int i = 0;i < num;i++){
            if (i%2 == 0){
                OrderReturnReasonEntity orderReturnReasonEntity = new OrderReturnReasonEntity();
                orderReturnReasonEntity.setCreateTime(new Date());
                orderReturnReasonEntity.setId(1L);
                orderReturnReasonEntity.setName("哈哈哈"+i);
                // 发送消息 对象必须实现Serializable接口
                rabbitTemplate.convertAndSend("hello-java-exchange",
                        "hello.java",orderReturnReasonEntity,
                        new CorrelationData(UUID.randomUUID().toString()));
//				log.info("消息发送完成{}",orderReturnReasonEntity);
            }else {
//                OrderEntity orderEntity = new OrderEntity();
//                orderEntity.setOrderSn(UUID.randomUUID().toString());
//                rabbitTemplate.convertAndSend("hello-java-exchange",
//                        "hello.java",orderEntity,
//                        new CorrelationData(UUID.randomUUID().toString()));
            }

        }
        return "ok";
    }
}
